home *** CD-ROM | disk | FTP | other *** search
/ Kit PC World De Ampliacion De Windows 95 / Kit PC World de ampliacion de Windows 95.iso / internet / sweeper / samples / asyncstg / barrfact.cpp < prev    next >
C/C++ Source or Header  |  1995-12-03  |  4KB  |  189 lines

  1. #include "barrfact.h"
  2.  
  3. #include "barrobj.h"
  4.  
  5. #include "tchar.h"
  6.  
  7. ULONG g_dwRefCount=0;
  8.  
  9. // Create a new database object and return a pointer to it
  10. HRESULT CAsyncByteArrayFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void** ppObject) 
  11. {
  12.     if (pUnkOuter && riid!=IID_IUnknown)
  13.     {
  14.         *ppObject=NULL;
  15.         return E_INVALIDARG;
  16.     }
  17.     CAsyncByteArray* pObj=new CAsyncByteArray();
  18.     HRESULT hRes=pObj->Initialize(pUnkOuter);
  19.     if (FAILED(hRes)) 
  20.     {
  21.         delete pObj;
  22.         return hRes;
  23.     }
  24.     
  25.     if (pUnkOuter)
  26.     {
  27.         *ppObject=(IUnknown*) (pObj->m_punkInner);
  28.         pObj->m_punkInner->AddRef();
  29.     }
  30.     else if (FAILED(pObj->QueryInterface(riid, ppObject))) 
  31.     {
  32.         delete pObj;
  33.         *ppObject=NULL;
  34.         return E_NOINTERFACE;
  35.     }
  36.     return S_OK;
  37. }
  38.  
  39. HRESULT    CAsyncByteArrayFactory::LockServer(BOOL fLock) 
  40. {
  41.     if (fLock) 
  42.     {
  43.         InterlockedIncrement((long*) &g_dwRefCount);
  44.     }
  45.     else 
  46.     {
  47.         InterlockedDecrement((long*) &g_dwRefCount);
  48.     }
  49.     return S_OK;
  50. }
  51.  
  52. CAsyncByteArrayFactory::CAsyncByteArrayFactory() 
  53. {
  54.     m_dwRefCount=0;
  55. }
  56.  
  57. HRESULT CAsyncByteArrayFactory::QueryInterface(REFIID riid, void** ppObject) 
  58. {
  59.     if (riid==IID_IUnknown || riid==IID_IClassFactory) 
  60.     {
  61.         *ppObject=(IClassFactory*) this;
  62.     }
  63.     else 
  64.     {
  65.         return E_NOINTERFACE;
  66.     }
  67.     AddRef();
  68.     return S_OK;
  69. }
  70.  
  71. ULONG CAsyncByteArrayFactory::AddRef() 
  72. {
  73.     InterlockedIncrement((long*) &g_dwRefCount);
  74.     InterlockedIncrement((long*) &m_dwRefCount);
  75.     return m_dwRefCount;
  76. }
  77.  
  78. ULONG CAsyncByteArrayFactory::Release() 
  79. {
  80.     ULONG dwRefCount=m_dwRefCount-1;
  81.     InterlockedDecrement((long*) &g_dwRefCount);
  82.     if (InterlockedDecrement((long*) &m_dwRefCount)==0) 
  83.     {
  84.         delete this;
  85.         return 0;
  86.     }
  87.     return dwRefCount;
  88. }
  89.  
  90. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void** ppObject) 
  91. {
  92.     if (rclsid==CLSID_AsyncByteArrayWrapper) 
  93.     {
  94.         CAsyncByteArrayFactory *pFactory= new CAsyncByteArrayFactory;
  95.         if (FAILED(pFactory->QueryInterface(riid, ppObject))) 
  96.         {
  97.             delete pFactory;
  98.             *ppObject=NULL;
  99.             return E_INVALIDARG;
  100.         }
  101.     }
  102.     else 
  103.     { // here you could check for additional CLSID's you DLL may provide
  104.         return CLASS_E_CLASSNOTAVAILABLE;
  105.     }
  106.     return NO_ERROR;
  107. }
  108.  
  109. HRESULT _stdcall DllCanUnloadNow() 
  110. {
  111.     if (g_dwRefCount) 
  112.     {
  113.         return S_FALSE;
  114.     }
  115.     else 
  116.     {
  117.         return S_OK;
  118.     }
  119. }
  120.  
  121. STDAPI DllRegisterServer(void) 
  122. {
  123.     HKEY hKeyCLSID, hKeyInproc32;
  124.     DWORD dwDisposition;
  125.  
  126.     if (RegCreateKeyEx(HKEY_CLASSES_ROOT, 
  127.             _T("CLSID\\{30DF3437-0266-11cf-BAA6-00AA003E0EED}"), 
  128.             NULL, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, 
  129.             &hKeyCLSID, &dwDisposition)!=ERROR_SUCCESS) 
  130.     {
  131.         return E_UNEXPECTED;
  132.     }
  133.  
  134.     if (RegSetValueEx(hKeyCLSID, "", NULL, REG_SZ, (BYTE*) "Asynchronous Byte Array Wrapper", sizeof("Asynchronous Byte Array Wrapper"))!=ERROR_SUCCESS) 
  135.     {
  136.         RegCloseKey(hKeyCLSID);
  137.         return E_UNEXPECTED;
  138.     }
  139.  
  140.     HMODULE hModule;
  141.     if (RegCreateKeyEx(hKeyCLSID, 
  142.             "InprocServer32", 
  143.             NULL, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, 
  144.             &hKeyInproc32, &dwDisposition)!=ERROR_SUCCESS) 
  145.     {
  146.         RegCloseKey(hKeyCLSID);
  147.         return E_UNEXPECTED;
  148.     }
  149.  
  150.     hModule=GetModuleHandle(_T("ASYNCSTG.DLL"));
  151.     if (!hModule) 
  152.     {
  153.         RegCloseKey(hKeyInproc32);
  154.         RegCloseKey(hKeyCLSID);
  155.         return E_UNEXPECTED;
  156.     }
  157.     TCHAR szName[MAX_PATH+1];
  158.     if (GetModuleFileName(hModule, szName, sizeof(szName))==0) 
  159.     {
  160.         RegCloseKey(hKeyInproc32);
  161.         RegCloseKey(hKeyCLSID);
  162.         return E_UNEXPECTED;
  163.     }
  164.     if (RegSetValueEx(hKeyInproc32, "", NULL, REG_SZ, (BYTE*) szName, sizeof(TCHAR)*(lstrlen(szName)+1))!=ERROR_SUCCESS) 
  165.     {
  166.         RegCloseKey(hKeyInproc32);
  167.         RegCloseKey(hKeyCLSID);
  168.         return E_UNEXPECTED;
  169.     }
  170.     RegCloseKey(hKeyInproc32);
  171.     RegCloseKey(hKeyCLSID);
  172.     return NOERROR;
  173. }
  174.  
  175. STDAPI DllUnregisterServer(void) 
  176. {
  177.     if (RegDeleteKey(HKEY_CLASSES_ROOT, 
  178.             _T("CLSID\\{30DF3437-0266-11cf-BAA6-00AA003E0EED}\\InprocServer32"))!=ERROR_SUCCESS) 
  179.     {
  180.         return E_UNEXPECTED;
  181.     }
  182.     if (RegDeleteKey(HKEY_CLASSES_ROOT, 
  183.             _T("CLSID\\{30DF3437-0266-11cf-BAA6-00AA003E0EED}"))!=ERROR_SUCCESS) 
  184.     {
  185.         return E_UNEXPECTED;
  186.     }
  187.     return NOERROR;
  188. }
  189.